package com.vteba.tx.jdbc.sequence;

import org.springframework.beans.factory.InitializingBean;

/**
 * 16进制/36进制的key产生器，通过表模拟。
 * @author yinlei
 * @date 2015年10月28日 上午11:27:43
 */
public class HexademicalKeyGenerator implements InitializingBean {
	private AbstractKeyGenerator keyGenerator;
	private Integer hex = 16; // 进制，16或者36进制
	private boolean upperCase = false;

	public AbstractKeyGenerator getKeyGenerator() {
		return keyGenerator;
	}

	public void setKeyGenerator(AbstractKeyGenerator keyGenerator) {
		this.keyGenerator = keyGenerator;
	}

	public Integer getHex() {
		return hex;
	}

	public void setHex(Integer hex) {
		this.hex = hex;
	}

	public String next() {
		long l = keyGenerator.nextLong();
		String s = Long.toString(l, hex);
		if (upperCase) {
			return s.toUpperCase();
		}
		return s;
	}
	
	public long nextLong() {
		return keyGenerator.nextLong();
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		if (keyGenerator == null) {
			throw new IllegalArgumentException("property [keyGenerator] cannot be null.");
		}
	}

	public boolean isUpperCase() {
		return upperCase;
	}

	public void setUpperCase(boolean upperCase) {
		this.upperCase = upperCase;
	}

}
